package red.ouo.distributedtools.redis.service;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import red.ouo.distributedtools.redis.message.config.RedisMessageConfig;
import red.ouo.distributedtools.standard.callback.Callback;
import red.ouo.distributedtools.standard.callback.CallbackStatus;
import red.ouo.distributedtools.standard.message.MessageBody;
import red.ouo.distributedtools.redis.message.core.RedisMessageCore;

import javax.annotation.Resource;

@Component
public class RedisMessage {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    RedisMessageConfig config;

    @Component
    public class Sender<T> {
        RedisMessageCore<T> end = RedisMessageCore.connect(redisTemplate,config).options().build();

        public void send(String queueName, MessageBody<T> messageBody) {
            end.queueName = queueName;
            end.send(messageBody);
        }
    }

    @Component
    public class Handler<T> {
        RedisMessageCore<T> end = RedisMessageCore.connect(redisTemplate,config).options().build();

        public void fetchOne(String queueName, Callback<MessageBody<T>> callback) {
            end.queueName = queueName;
            end.fetchUnitary(callback);
        }
        public void fetchSome(String queueName,Integer count, Callback<MessageBody<T>> callback) {
            end.queueName = queueName;
            end.fetchSomeTimeOut((m,t)->{
                callback.ok(m,t);
                return true;
            },count,0);
        }
        public void fetchSomeHasStatus(String queueName,Integer count, CallbackStatus<MessageBody<T>> callback) {
            end.queueName = queueName;
            end.fetchSomeTimeOut(callback,count,0);
        }
    }
}
